﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Public;
using BLL;

namespace QLNhanKhau
{
    public partial class frmChuyenDen : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmChuyenDen()
        {
            InitializeComponent();
        }

        private void btnTimSoChuyenKhau_Click(object sender, EventArgs e)
        {
            int SoChuyenKhau = int.Parse(txtSoChuyenKhau.Text);

            List<ChuyenKhauPublic> list_cd = ChuyenKhauBLL.LoadChuyenKhauByMaNK(SoChuyenKhau);

            dgvChuyenDen.DataSource = list_cd;

            //dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
            dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
            //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
            dgvNhanKhau.Columns[4].HeaderText = "CMND";
            dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
            //dgvNhanKhau.Columns[6].HeaderText = "Nơi sinh";
            dgvNhanKhau.Columns[7].HeaderText = "Giới tính";
            dgvNhanKhau.Columns[8].HeaderText = "Quê quán";
            dgvNhanKhau.Columns[9].HeaderText = "Dân tộc";
            dgvNhanKhau.Columns[10].HeaderText = "Nghề nghiệp";
            dgvNhanKhau.Columns[11].HeaderText = "Quốc tịch";
            //dgvNhanKhau.Columns[12].HeaderText = "Chứng tử";
            dgvNhanKhau.Columns[13].HeaderText = "Tôn giáo";
            dgvNhanKhau.Columns[14].HeaderText = "Trình độ";
            //dgvNhanKhau.Columns[15].HeaderText = "Sổ hộ khẩu";
            dgvNhanKhau.Columns[16].HeaderText = "Quan hệ";
            dgvNhanKhau.Columns[0].Visible = false;
            //dgvNhanKhau.Columns[1].Visible = false;
            dgvNhanKhau.Columns[2].Visible = false;
            dgvNhanKhau.Columns[3].Visible = false;
            //dgvNhanKhau.Columns[4].Visible = false;
            //dgvNhanKhau.Columns[5].Visible = false;
            dgvNhanKhau.Columns[6].Visible = false;
            //dgvNhanKhau.Columns[7].Visible = false;
            //dgvNhanKhau.Columns[8].Visible = false;
            dgvNhanKhau.Columns[9].Visible = false;
            //dgvNhanKhau.Columns[10].Visible = false;
            dgvNhanKhau.Columns[11].Visible = false;
            dgvNhanKhau.Columns[12].Visible = false;
            dgvNhanKhau.Columns[13].Visible = false;
            dgvNhanKhau.Columns[14].Visible = false;
            dgvNhanKhau.Columns[15].Visible = false;
            //dgvNhanKhau.Columns[16].Visible = false;
        }

        private void cmbNguoiNhan_SelectionChangeCommitted(object sender, EventArgs e)
        {
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVuChuyenDen.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVuChuyenDen.Enabled = false;
        }

        private void dgvChuyenDen_SelectionChanged(object sender, EventArgs e)
        {
            //NhanKhauPublic nk=new NhanKhauPublic();
            DataGridViewCell cell = null;

            foreach (DataGridViewCell select in dgvChuyenDen.SelectedCells)
            {
                cell = select;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;

                NhanKhauPublic nk = NhanKhauBLL.LoadNhanKhauByMaNK((int)row.Cells[1].Value);
                //txtHoTen.Text=
                txtSoChuyenDi.Text = (string)row.Cells[0].Value;
                txtSoSHKDen.Text = (string)row.Cells[3].Value;
                //txtChuHoChuyenDen.Text=(string)row

            }

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            KhaiSinhPublic ks = new KhaiSinhPublic();
            ks.MaKS = DungChung.LoadMaNK();
            ks.NoiSinh = txtNoiSinh.Text;
            KhaiSinhBLL.InsertKhaiSinh(ks);

            NhanKhauPublic nk = new NhanKhauPublic();
            nk.MaNK = ks.MaKS;
            nk.HoTen = txtHoTen.Text;
            if (rdtGioiTinhNam.Checked)
                nk.GioiTinh = true;
            else
                nk.GioiTinh = false;
            nk.TenGoiKhac = txtTenGoiKhac.Text;
            nk.NgaySinh = DateTime.Parse(dateNgaySinh.Text);
            nk.CMND = txtCMND.Text;
            nk.NgheNghiep = txtNgheNghiep.Text;
            nk.MaTD = (byte)cmbTrinhDo.SelectedValue;
            nk.TenGoiKhac = txtTenGoiKhac.Text;
            nk.MaTG = (byte)cmbTonGiao.SelectedValue;
            nk.MaQT = (byte)cmbQuocTich.SelectedValue;
            nk.MaDT = (byte)cmbDanToc.SelectedValue;
            nk.MaKS = ks.MaKS;
            nk.MaSHK = SoHoKhauBLL.LoadSoHoKhauBySoSHK(int.Parse(txtSoSHKDen.Text)).MaSHK;
            nk.QueQuan = txtQueQuan.Text;
            nk.MaQH = (byte)cmbQuanHe.SelectedValue;
            NhanKhauBLL.InsertNhanKhau(nk);
            //frmChuyenDen_Load(sender, e);

            //themChuyenKhau
            ChuyenKhauPublic ck = new ChuyenKhauPublic();
            ck.SoChuyenKhau = int.Parse(txtSoChuyenDi.Text);
            ck.LyDoChuyen = txtLyDoChuyen.Text;
            //ck.MaCK = 00;
            ck.NgayCap = DateTime.Parse(dateNgayCap.Text);
            ck.MaNK = DungChung.LoadMaNK();
            //ck.NgayChuyen = DateTime.Parse(dateNgayDi.Text);
            ck.NgayNhan = dateNgayDen.Value;
            ck.NguoiCap = txtNguoiCap.Text;
            ck.NguoiNhan = cmbNguoiNhan.Text;
            ck.NoiCap = int.Parse(cmbPhuongXa.SelectedValue.ToString() + cmbQuanHuyen.SelectedValue.ToString()  + cmbTinh.SelectedValue.ToString());
            //ck.NoiChuyen
            int macden = ChuyenKhauBLL.LoadMaCDen((int)cmbPhuongXa.SelectedValue)+1;
            string chuoiMaCDen = macden.ToString();
            if ((int)cmbPhuongXa.SelectedValue < 10)
            {
                if (chuoiMaCDen.Length < 6)
                    chuoiMaCDen = "0" + (int)cmbPhuongXa.SelectedValue + "00000" + chuoiMaCDen;
            }
            else
            {
                if (chuoiMaCDen.Length < 6)
                    chuoiMaCDen = (int)cmbPhuongXa.SelectedValue + chuoiMaCDen;
            }
            ck.MaCK = int.Parse(chuoiMaCDen);
            ck.NguoiNhan = cmbNguoiNhan.Text;
            //ck.NgayDen = dateNgayDen.Value;
            ck.CanBoNoiNhan = cmbCanBoDangKi.Text;
            ck.ChucVuNguoiChuyen = txtChucVuChuyenDen.Text;
            ck.ChucVuNguoiNhan = txtChucVu.Text;
            ck.CanBoNoiChuyen="";
            ChuyenKhauBLL.InsertChuyenKhau(ck);
        }

        private void cmbTinh_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cmbQuanHuyen.DataSource = QuanHuyenBLL.LoadQuanHuyenByMaT((byte)cmbTinh.SelectedValue);
            cmbQuanHuyen.DisplayMember = "TenQuanHuyen";
            cmbQuanHuyen.ValueMember = "MaQH";
        }

        private void cmbQuanHuyen_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cmbPhuongXa.DataSource = PhuongXaBLL.LoadPhuongXaByMaQH((byte)cmbQuanHuyen.SelectedValue);
            cmbPhuongXa.DisplayMember = "TenPhuongXa";
            cmbPhuongXa.ValueMember = "MaPX";
        }

        private void frmChuyenDen_Load(object sender, EventArgs e)
        {

            MessageBox.Show("gesgaesg");
            cmbNguoiNhan.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiNhan.DisplayMember = "HoTen";
            cmbNguoiNhan.ValueMember = "MaND";

            cmbCanBoDangKi.Text = frmDangNhap.user_login.HoTen;

            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue);
            txtChucVuChuyenDen.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;

            dateNgayDen.Text = DateTime.Now.ToString();


            cmbTinh.DataSource = TinhBLL.LoadTinh();
            cmbTinh.DisplayMember = "TenTinh";
            cmbTinh.ValueMember = "MaT";

            cmbTinh.SelectedIndex = -1;

            cmbTinhDen.DataSource = TinhBLL.LoadTinh();
            cmbTinhDen.DisplayMember = "TenTinh";
            cmbTinhDen.ValueMember = "MaT";

            cmbTinhDen.SelectedIndex = -1;
        }

        private void cmbTinhDen_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cmbQuanHuyenDen.DataSource = QuanHuyenBLL.LoadQuanHuyenByMaT((byte)cmbTinh.SelectedValue);
            cmbQuanHuyenDen.DisplayMember = "TenQuanHuyen";
            cmbQuanHuyenDen.ValueMember = "MaQH";
        }

        private void cmbQuanHuyenDen_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cmbPhuongXaDen.DataSource = PhuongXaBLL.LoadPhuongXaByMaQH((byte)cmbQuanHuyen.SelectedValue);
            cmbPhuongXaDen.DisplayMember = "TenPhuongXa";
            cmbPhuongXaDen.ValueMember = "MaPX";
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Thoát không lưu", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                this.MdiParent = null;
                this.Dispose();

            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (txtSoSHKDen.Text == "")
            {
                MessageBox.Show("Nhập số sổ hộ khẩu cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int SoSHK = int.Parse(txtSoSHKDen.Text);
            SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
            if (shk == null)
            {
                txtDuong.Text = txtAp.Text = txtSoNha.Text = txtChuHo.Text = "";
                dgvNhanKhau.DataSource = null;
                groupBox1.Enabled = false;
                groupBox2.Enabled = false;
                MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                //SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
                List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);

                groupBox1.Enabled = true;
                groupBox2.Enabled = true;
                txtChuHo.Text = list_nk.Find(item => item.MaQH == 1).HoTen;
                dgvNhanKhau.DataSource = list_nk;
                dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
                dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
                //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
                dgvNhanKhau.Columns[4].HeaderText = "CMND";
                dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
                dgvNhanKhau.Columns[6].HeaderText = "Giới tính";
                dgvNhanKhau.Columns[7].HeaderText = "Quê quán";
                dgvNhanKhau.Columns[8].HeaderText = "Dân tộc";
                dgvNhanKhau.Columns[9].HeaderText = "Nghề nghiệp";
                dgvNhanKhau.Columns[10].HeaderText = "Quốc tịch";
                //dgvNhanKhau.Columns[11].HeaderText = "Chứng tử";
                dgvNhanKhau.Columns[12].HeaderText = "Tôn giáo";
                dgvNhanKhau.Columns[13].HeaderText = "Trình độ";
                //dgvNhanKhau.Columns[14].HeaderText = "Sổ hộ khẩu";
                dgvNhanKhau.Columns[15].HeaderText = "Quan hệ";
                //dgvNhanKhau.Columns[0].Visible = false;
                //dgvNhanKhau.Columns[1].Visible = false;
                dgvNhanKhau.Columns[2].Visible = false;
                dgvNhanKhau.Columns[3].Visible = false;
                //dgvNhanKhau.Columns[4].Visible = false;
                //dgvNhanKhau.Columns[5].Visible = false;
                //dgvNhanKhau.Columns[6].Visible = false;
                //dgvNhanKhau.Columns[7].Visible = false;
                dgvNhanKhau.Columns[8].Visible = false;
                //dgvNhanKhau.Columns[9].Visible = false;
                dgvNhanKhau.Columns[10].Visible = false;
                dgvNhanKhau.Columns[11].Visible = false;
                dgvNhanKhau.Columns[12].Visible = false;
                dgvNhanKhau.Columns[13].Visible = false;
                dgvNhanKhau.Columns[14].Visible = false;
                //dgvNhanKhau.Columns[15].Visible = false;

                //load phần sổ hộ khẩu
                txtDuong.Text = shk.Duong;
                txtAp.Text = shk.KhuPho.ToString();
                txtSoNha.Text = shk.SoNha;
            }
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.ForeColor = Color.AliceBlue;
            style.BackColor = Color.Red;
            for (int i = dgvNhanKhau.RowCount - 1; i >= 0; i--)
            {
                if ((int)dgvNhanKhau.Rows[i].Cells[11].Value == 0)
                {
                    //MessageBox.Show("geg");
                    dgvNhanKhau.Rows[i].DefaultCellStyle = style;
                }
                //if ((bool)dgvNguoiDung.Rows[i].Cells[5].Value==true)
                //{
                //  dgvNguoiDung.Rows[i].DefaultCellStyle = style;
                //}

            }
        }

        private void txtSoSHK_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void txtSoChuyenKhau_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        
    }
}
